package com.longrise.android.byjk.plugins.vip.dailysign;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.event.IntegralTaskEvent;
import com.longrise.android.byjk.event.MineRefreshEvent;
import com.longrise.android.byjk.event.MyIntegralEvent;
import com.longrise.android.byjk.plugins.publicWeb.PublicWebJumpUtil;
import com.longrise.android.byjk.plugins.vip.integralexchange.IntegralTaskUtil;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.common.utils.AppUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by callxkj on 2017/4/14.
 * 每日签到
 */

public class DailySignActivity extends BaseActivity<DailySignPresenter>
        implements AppBarLayout.OnOffsetChangedListener, DailySignContract.View, View.OnClickListener {
    public static final String TAG = "DailySignActivity";
    public static final String SIGN_STATUS = "SIGN_STATUS";
    public static final String TO_SIGN = "TO_SIGN";

    private AppBarLayout mAppBar;
    private Toolbar mToolbar;
    private LinearLayout mDaysll;
    private RecyclerView mRcv;
    private DailySignRcvAdapter mAdapter;
    private TextView mTvqdts;
    private TextView mTvwdjf;
    private Dialog mSignSuccDialog;
    private TextView mTvqdgz;
    private View mFooterView;
    private RelativeLayout mRlzqgdjf;
    private String mFromPagename;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_dailysign;
    }

    @Override
    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.dailysign_bb_toolbar);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        mRcv = (RecyclerView) findViewById(R.id.dailysign_rcv);
        mAppBar = (AppBarLayout) findViewById(R.id.dailysign_appbar);
        mDaysll = (LinearLayout) findViewById(R.id.dailysign_days_ll);
        mTvqdts = (TextView) findViewById(R.id.dailysign_qdts_tv);
        mTvwdjf = (TextView) findViewById(R.id.dailysign_wdjf_total_tv);
        mTvqdgz = (TextView) findViewById(R.id.dailysign_qdgz_tv);
        mFooterView = View.inflate(mContext, R.layout.item_dailysign_rcv_foot, null);
        mRlzqgdjf = (RelativeLayout) mFooterView.findViewById(R.id.dailysign_foot_zqgdjf);
        initRcv();
        initEvent();
        toGetIntent();

    }

    @Override
    public void initTheme() {
        setTheme(R.style.AppCompatThemDark);
    }

    private void initRcv() {
        mAdapter = new DailySignRcvAdapter();
        mAdapter.addFooterView(mFooterView);
        mRcv.setLayoutManager(new LinearLayoutManager(mContext));
        mRcv.setAdapter(mAdapter);
    }

    private void initEvent() {
        mRlzqgdjf.setOnClickListener(this);
        mTvqdgz.setOnClickListener(this);
        mAppBar.addOnOffsetChangedListener(this);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void toGetIntent() {
        Intent intent = getIntent();
//        mFromPagename = intent.getStringExtra(BBWebHelper.FROM_PAGE);
        mPresenter.refreshData(true);
    }

    /**
     * appbar 展开收拢
     */
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset == 0) {
            mToolbar.setBackgroundColor(Color.TRANSPARENT);
        } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
            mToolbar.setBackgroundResource(R.drawable.bg_act_my_user);
        } else {
            mToolbar.setBackgroundResource(R.drawable.bg_act_my_user);
        }
    }

    @Override
    public void showSignSuccessDialog(String continuitydays, String acquiredscore, String totalscores) {
        View dialogView = View.inflate(mContext, R.layout.dialog_sign_signsucc, null);
        TextView tv_wzdl = (TextView) dialogView.findViewById(R.id.dialog_sign_succ_wzdl_tv);
        TextView tv_zqjf = (TextView) dialogView.findViewById(R.id.dialog_sign_succ_zqjf_tv);
        TextView tv_lxqd = (TextView) dialogView.findViewById(R.id.dialog_sign_succ_lxqd_tv);
        TextView tv_hdjf = (TextView) dialogView.findViewById(R.id.dialog_sign_succ_hdjf_tv);
        TextView tv_dazjf = (TextView) dialogView.findViewById(R.id.dialog_sign_succ_dqzjf_tv);
        tv_lxqd.setText(continuitydays);
        tv_hdjf.setText("恭喜您获得 " + acquiredscore + " 分");
        tv_dazjf.setText("您当前总积分为 " + totalscores);
        tv_wzdl.setOnClickListener(this);
        tv_zqjf.setOnClickListener(this);

        //弹窗说明积分已经变化
        MineRefreshEvent event = new MineRefreshEvent();
        event.setBBye(true);
        event.setName(true);//有可能更改昵称
        EventBus.getDefault().post(event); //刷新我的页面
        EventBus.getDefault().post(new MyIntegralEvent(true)); //刷新我的积分页面

        IntegralTaskEvent event2 = new IntegralTaskEvent();
        event2.setRefresh(true);
        EventBus.getDefault().post(event2);//刷新积分任务

        mSignSuccDialog = DialogUtil.getInstance().creatAlertDialog(mContext, dialogView, 270, 355);
        mSignSuccDialog.show();
    }

    @Override
    public void showUserScores(String continuitydays, String vipscores, EntityBean[] qdInfoses) {
        mTvqdts.setText(continuitydays); //签到天数
        mTvwdjf.setText("我的积分：" + vipscores); //我的总积分
        if (qdInfoses == null) {
            return;
        }
        for (int x = 0; x < qdInfoses.length; x++) {
            View view = View.inflate(mContext, R.layout.item_dailysign_days, null);
            //-------布局--------------
            RelativeLayout viewll = (RelativeLayout) view.findViewById(R.id.item_dailysign_ll);
            RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.item_dailysign_rl);
            View view_line = view.findViewById(R.id.item_dailysign_line);
            View view_circle = view.findViewById(R.id.item_dailysign_circle);
            double screenwidth = AppUtil.getScreenWidth();
            ViewGroup.LayoutParams layoutParams = viewll.getLayoutParams();
            if (x == qdInfoses.length - 1) {
                layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
                ViewGroup.LayoutParams lp_line = view_line.getLayoutParams();
                lp_line.width = AppUtil.dip2px(20);
                view_line.setLayoutParams(lp_line);

                ViewGroup.LayoutParams lp_rl = rl.getLayoutParams();
                lp_rl.width = LinearLayout.LayoutParams.WRAP_CONTENT;
                rl.setLayoutParams(lp_rl);

            } else {
                layoutParams.width = (int) (screenwidth / 7.5);
            }
            //---------------------------
            TextView tv_scores = (TextView) view.findViewById(R.id.item_dailysign_scores);
            TextView tv_days = (TextView) view.findViewById(R.id.item_dailysign_days);
            EntityBean bean = qdInfoses[x];
            tv_scores.setText("+" + bean.getString("scores"));
            tv_days.setText(bean.getString("days") + "天");
            String qdstate = bean.getString("qdstate"); //1表示已经签到 0未签到
            if ("1".equals(qdstate)) {
                view_circle.setSelected(true);
                view_line.setSelected(true);
                tv_scores.setTextColor(Color.parseColor("#FFFFFF"));
                tv_scores.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
                tv_days.setTextColor(Color.parseColor("#99FFFFFF"));
            } else {
                view_circle.setSelected(false);
                view_line.setSelected(false);
                tv_scores.setTextColor(Color.parseColor("#CC6804"));
                tv_scores.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
                tv_days.setTextColor(Color.parseColor("#CC6804"));
            }
            view.setLayoutParams(layoutParams);
            mDaysll.addView(view);
        }
    }

    @Override
    public void showScoresrank(EntityBean[] rankbeans) {
        mAdapter.refreshData(rankbeans);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_sign_succ_wzdl_tv: //我知道了
                DialogUtil.getInstance().dismiss();
                break;
            case R.id.dialog_sign_succ_zqjf_tv:  //赚取积分 积分任务
                DialogUtil.getInstance().dismiss();
                IntegralTaskUtil.toIntegralTask(this);
                break;
            case R.id.dailysign_qdgz_tv: //签到规则
               // PublicWebJumpUtil.jumpToPubWeb(this, "qdgz");
                break;
            case R.id.dailysign_foot_zqgdjf: //赚取更多积分
                IntegralTaskEvent event2 = new IntegralTaskEvent();
                event2.setRefresh(true);
                EventBus.getDefault().post(event2);//刷新积分任务
                IntegralTaskUtil.toIntegralTask(this);
                break;
        }
    }

}
